<?php
class Model_Mediate_CaseMediateLog extends Model_BaseModel
{
    use Lib_BaseTraits;
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_Mediate_CaseMediateLog';
    /**
     * Undocumented variable
     *
     * @var string
     */
    private $table = "case_mediation";

    public function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }

    // 根据用户case_id获取表名
    public function index_table_name($case_id)
    {
        $index_table_name = 'case_mediation_' . intval($case_id) % 20;
        return $index_table_name;
    }

    /**添加
     * @param $insertdata
     * @return mixed
     */

    public function insert($mediate_data,$update_case)
    {
        $this->dao->conn(false);
        $this->dao->noCache();
        $this->dao->beginTransaction();
        $child_table = $this->index_table_name($mediate_data['caseId']);
        $data = array();

        $sql = "INSERT INTO {$child_table} (";
        $sql .= '`' . implode('`,`', array_keys($mediate_data)) . '`';
        $sql .= ')VALUES(:' . implode(',:', array_keys($mediate_data)) . ')';
        foreach ($mediate_data as $key => $value) {
            $data[':' . $key] = $value;
        }
        $inser_res = $this->dao->preparedSql($sql, $data)->lastInsertId();
        if (!$inser_res) {
            $this->dao->rollback();
            return false;
        }
        if($update_case){
            $sql = 'select caseLabel,cardId,importRecordId from `cases` where id = :id';
            $case_record = $this->dao->preparedSql($sql, [':id'=>$mediate_data['caseId']])->fetchOne();
            if (!$case_record) {
                $this->dao->rollback();
                return false;
            }
            if($case_record['caseLabel'] == 1){
                //修改状态
                $sql = 'UPDATE `cases` SET ';
                $sqlarr = array();
                foreach ($update_case as $key => $value) {
                    array_push($sqlarr, "`{$key}`=:$key");
                    $case[":$key"] = $value;
                }
                $sql .= implode(',', $sqlarr);
                $sql .= ' WHERE importRecordId=:importRecordId and cardId = :cardId';
                $case[':importRecordId'] = $case_record['importRecordId'];
                $case[':cardId'] = $case_record['cardId'];
                $update_res = $this->dao->preparedSql($sql, $case)->affectedCount();
            }else {
                //修改状态

                $sql = 'UPDATE `cases` SET ';
                $sqlarr = array();
                foreach ($update_case as $key => $value) {
                    array_push($sqlarr, "`{$key}`=:$key");
                    $case[":$key"] = $value;
                }
                $sql .= implode(',', $sqlarr);
                $sql .= ' WHERE id=:id';
                $case[':id'] = $mediate_data['caseId'];
                $update_res = $this->dao->preparedSql($sql, $case)->affectedCount();
            }
            if (!$update_res) {
                $this->dao->rollback();

                return false;
            }
        }
        // 结束事务
        $this->dao->commit();
        $this->dao->clearTag(self::$tag);
        $this->dao->clearTag('Model_Mediate_Case');

        return $inser_res;
    }

    //根据第三方案件id获取数据

    public function getOneByThirdId($caseId,$callId){
        $child_table = $this->index_table_name($caseId);
        $sql = "SELECT * FROM {$child_table} WHERE caseId = :caseId AND callId =:callId";
        $data[':caseId'] = $caseId;
        $data[':callId'] = $callId;
        return $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->fetchOne();
    }
    /**
     * 修改数据
     */
    public function update($mediate_data,$update_case,$id)
    {
        $this->dao->conn(false);
        $this->dao->noCache();
        $this->dao->beginTransaction();

        $child_table = $this->index_table_name($mediate_data['caseId']);
        $data = [];
        $sql = "UPDATE {$child_table} SET ";
        $sqlarr = array();
        foreach ($mediate_data as $key => $value) {
            array_push($sqlarr, "`{$key}`=:$key");
            $data [":$key"] = $value;
        }
        $sql .= implode(',', $sqlarr);
        $sql .= " WHERE id=:id";
        $data[':id'] = $id;


        $res = $this->dao->preparedSql($sql, $data)->affectedCount();
        if (!$res) {
            $this->dao->rollback();

            return false;
        }

        if($update_case){
            //查询案件是否多个案件
            $sql = 'select caseLabel,cardId,importRecordId from `cases` where id = :id';
            $case_record = $this->dao->preparedSql($sql, [':id'=>$mediate_data['caseId']])->fetchOne();
            if (!$case_record) {
                $this->dao->rollback();
                return false;
            }
            if($case_record['caseLabel'] == 1){
                //修改状态
                $sql = 'UPDATE `cases` SET ';
                $sqlarr = array();
                foreach ($update_case as $key => $value) {
                    array_push($sqlarr, "`{$key}`=:$key");
                    $case[":$key"] = $value;
                }
                $sql .= implode(',', $sqlarr);
                $sql .= ' WHERE importRecordId=:importRecordId and cardId = :cardId';
                $case[':importRecordId'] = $case_record['importRecordId'];
                $case[':cardId'] = $case_record['cardId'];
                $update_res = $this->dao->preparedSql($sql, $case)->affectedCount();
            }else{
                //修改状态
                $sql = 'UPDATE `cases` SET ';
                $sqlarr = array();
                foreach ($update_case as $key => $value) {
                    array_push($sqlarr, "`{$key}`=:$key");
                    $case[":$key"] = $value;
                }
                $sql .= implode(',', $sqlarr);
                $sql .= ' WHERE id=:id';
                $case[':id'] = $mediate_data['caseId'];

                $update_res = $this->dao->preparedSql($sql, $case)->affectedCount();
            }

            if (!$update_res) {
                $this->dao->rollback();

                return false;
            }
        }
        // 结束事务
        $this->dao->commit();
        $this->dao->clearTag(self::$tag);
        $this->dao->clearTag('Model_Mediate_Case');

        $this->dao->clearTag(self::$tag);


        return $res;
    }

    //获取调解记录
    public function getMediateLogByCaseId($caseId,$where=[]){
        $child_table = $this->index_table_name($caseId);
        $sql = "SELECT * FROM {$child_table} WHERE caseId = :caseId";
        if($where){
            if(isset($where) && !empty($where)){
                $sql .= ' AND '.implode(' AND ',$where);
            }
        }
        $sql .=" ORDER BY id DESC";
        $data = [':caseId' => $caseId];
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchAll();
    }
}
